package com.power.leetcode;

import java.util.Stack;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/7 6:59 下午
 */
public class Solution_150 {

    Stack stack = new Stack();

    public int evalRPN(String[] tokens) {
        if (tokens.length == 0 || tokens == null) {
            return -1;
        }

        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
                int value2 = (int) stack.pop();
                int value = (int) stack.pop();
                switch (tokens[i]) {
                    case "+":
                        stack.push(value + value2);
                        break;
                    case "-":
                        stack.push(value - value2);
                        break;
                    case "*":
                        stack.push(value * value2);
                        break;
                    case "/":
                        stack.push(value / value2);
                        break;
                    default:
                }
            } else {
                int value = Integer.parseInt(tokens[i]);
                stack.push(value);
            }
        }


        return (int)stack.pop();
    }
}